package src.leetcode.lcsaf.simple_1652;

import java.util.Arrays;

public class decrypt {
    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(Arrays.toString(solution.decrypt(new int[]{10, 5, 7, 7, 3, 2, 10, 3, 6, 9, 1, 6}, -4)));
    }
}


class Solution {
    public int[] decrypt(int[] code, int k) {
        int n = code.length;
        int[] ans = new int[n];
        if (k == 0) {
            return ans;
        }
        int sum = 0;
        for (int i = 0; i < n + Math.abs(k); i++) {
            sum += code[i % n];
            if (i < Math.abs(k) - 1) {
                continue;
            }
            if (k > 0) {
                ans[(i - k + n) % n] = sum;
            } else {
                ans[(i + 1) % n] = sum;
            }
            sum -= code[(i - Math.abs(k) + n + 1) % n];
        }
        return ans;
    }
}